﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace SharePoint.Contrib.Extensions
{
    /// <summary>
    /// Extension methods for the IEnumerable class
    /// </summary>
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// Executes the deferred execution of a LINQ query. The result is passed in the out parameter. 
        /// The method returns true/false depending on the whether the execution contains an element.
        /// </summary>
        public static bool Execute<T>(this IEnumerable<T> instance, out T result)
        {
            result = instance.FirstOrDefault();
            return Equals(result, default(T)) == false;
        }

        /// <summary>
        /// Executes the deferred execution of a LINQ query. The result is passed in the out parameter. 
        /// The method returns true/false depending on the whether the execution contains any elements.
        /// </summary>
        public static bool Execute<T>(this IEnumerable<T> instance, out IEnumerable<T> results)
        { 
            results = new List<T>(instance.DefaultIfEmpty());
            return Equals(results.First(), default(T)) == false;
        }

        /// <summary>
        /// Performs the specified action on each element in the specified collection
        /// </summary>
        /// <param name="action">The action (delegate) to run</param>
        public static void ForEach<T>(this IEnumerable<T> instance, Action<T> action)
        {
            foreach (T item in instance)
            {
                action(item);
            }
        }
 
      
    }
}
